package com.zombie.logic.object;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.zombie.Constants;
import com.zombie.Renderable;
import com.zombie.ResourceManager;
import com.zombie.logic.Material;
import com.zombie.logic.core.ZombieWorld;
import com.zombie.logic.enums.AnimationType;
import com.zombie.logic.enums.ObjectType;
import com.zombie.state.GameState;

public abstract class GameObject implements Renderable{

	public Body body;
	public ObjectType type = ObjectType.LIVE;
	public AnimationType animation = AnimationType.STAND;
	public LiveObject owner;

	public Vector2 position = new Vector2();
	public Vector2 oldPosition = new Vector2();
	public Vector2 size = new Vector2();
	public String material,image,name;
	public int renderGroup = Constants.GROUP_NORMAL;
	public float angle = 90;
	long birthTime = 0;
	private float velocity;
	public boolean wait = false;
	
	public GameObject(float x,float y){
		position.set(x, y);
		oldPosition.set(position);
		birthTime = GameState.time;
	}
	
	public void remove() {
		ZombieWorld.removeObject(this);
		removeBody();
	}
	
	private void removeBody() {
		if (body != null){
			ZombieWorld.world.destroyBody(body);
			body.setUserData(null);
			body = null;
		}
	}

	public void update(float delta){
		updatePosition();
	}

	public boolean isDead(){
		return false;
	}
	
	public void updatePosition(){
		oldPosition.set(position);
		if (body != null){
			position.set(body.getPosition()).mul(Constants.BOX_TO_WORLD);
		}
	}
	
	public void draw(SpriteBatch batch, ShapeRenderer shapeBatch) {
		TextureRegion sprite = ResourceManager.getImage(image);
		if (sprite == null)
			return;
		batch.draw(sprite, position.x-size.x/2, position.y-size.y/2, size.x/2, size.y/2, size.x, size.y, 1, 1, angle, true);
	}
	
	public void createBody(){
		
	}

	public float getVelocity() {
		return velocity;
	}

	public void setVelocity(float velocity) {
		this.velocity = velocity;
	}
	

	public float distanceToObject(GameObject obj) {
		return position.dst(obj.position);
	}
	

	public float getAngleToObject(GameObject obj) {
		return MathUtils.radDeg*MathUtils.atan2(obj.position.y-position.y, obj.position.x-position.x);
	}
	
	public Material getMaterial(){
		return Material.get(material);
	}

	public int getRenderGroup(){
		return renderGroup;
	}
}
